package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Dict;
import com.xmy.cultivate.entity.DictType;
import com.xmy.cultivate.service.IDictService;
import com.xmy.cultivate.service.IDictTypeService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.IdWorker;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典类别 前端控制器
 * </p>
 *
 * @author min
 * @since 2022-07-25
 */
@RestController
@RequestMapping("/api/dictType")
@Scope("prototype")
public class DictTypeController extends BaseController{
    @Autowired
    IDictTypeService iDictTypeService;

    @Autowired
    IdWorker idWorker;

    @GetMapping()
    public Result findAll(@RequestParam Map reMap){
        try{
            //StringUtils.isEmpty(reMap.get("pageNum"));
            IPage<DictType> page = new Page<>(1,1000);
            String type = (String) reMap.get("type");
            QueryWrapper<DictType> dictQueryWrapper =  new QueryWrapper<>();
            dictQueryWrapper.eq(StringUtils.isNotBlank(type),"type",type);

            iDictTypeService.page(page,dictQueryWrapper);
            List<DictType> dictList = page.getRecords();

            Map<String,Object> map = new HashMap<>();
            map.put("list",dictList);
            map.put("total",page.getTotal());
            map.put("pages",page.getPages());
            return new Result(Code.OK,map);
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }
}
